const app = getApp()
const publicFun = require('/utils/public.js')
import { getClimateActionList } from '/utils/request'

Page({
  data: {
    pageindex: 1,
    leftHeight: 0,
    rightHeight: 0,
    leftList: [],
    rightList: [],
    pageindex: 1,
  },
  onLoad(options) {
    this.setData({
      systemInfo: app.globalData.systemInfo,
      userid: options.userid ? options.userid : app.globalData.userid,
      typeid: options.typeid,
      zhouqitype: options.zhouqitype,
      leftList: [],
      rightList: [],
    })
    my.setNavigationBarColor({
      frontColor: '#000000',
      backgroundColor: '#ffffff',
    })
    my.showLoading();
    this.getClimateActionList(this.data.pageindex, this.data.typeid);
  },
  async getClimateActionList(pageindex, typeid) { 
    try{
      var params = {
        duserid: app.globalData.userid,
        airesult: '',
        userid: this.data.userid,
        typeid: '-'+typeid,
        zhouqitype: this.data.zhouqitype,
        isbangdan: 0,
        pagesize: app.globalData.pagesize,
        pageindex: pageindex
      }
      const res = await getClimateActionList(params)
      my.hideLoading();
      if(res.S == 1){
        let list = res.L;
        if(list.length > 0){
          this.wallList(list)
          this.setData({ HC: res.HC });
        }else{
          publicFun.showToast("已经加载到底啦")
        }
      }else{
        publicFun.showToast(res.M ? res.M : '加载失败');
      }
    }catch(e){
      my.hideLoading();
      publicFun.showToast('失败，请稍后再试')
    }
  },
  wallList(list) {
    var that = this;
    var allData = list;
    //定义两个临时的变量来记录左右两栏的高度，避免频繁调用setData方法
    var leftH = that.data.leftHeight;
    var rightH = that.data.rightHeight;
    var leftData = that.data.leftList;
    var rightData = that.data.rightList;

    for (let i = 0; i < allData.length; i++) {
      if (leftH == rightH || leftH < rightH) {//判断左右两侧当前的累计高度，来确定item应该放置在左边还是右边
        leftData.push(allData[i]);
        leftH += Number(allData[i].CoverHeight);
        // console.log('左边', Number(allData[i].CoverHeight),  '==', leftH)
      } else {
        rightData.push(allData[i]);
        rightH += Number(allData[i].CoverHeight);
        // console.log('右边', Number(allData[i].CoverHeight), rightH)
      }

      that.setData({
        leftHight: leftH,
        rightHight: rightH,
        leftList: leftData,
        rightList: rightData
      })

    }
  },
  
  onPullDownRefresh() {
    // 页面被下拉
  },
  onReachBottom(e) { //上拉加载
    const newPage = this.data.pageindex + 1;
    this.getClimateActionList(newPage, this.data.typeid);
    this.setData({pageindex: newPage});
  },
});
